package togoogle;

import java.util.HashMap;
import java.util.List;
import java.util.TreeMap;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import java.util.Map;
import java.util.logging.Logger;

import java.util.Date;
import org.mortbay.log.Log;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.Item;
import com.taobao.api.request.ItemsOnsaleGetRequest;
import com.taobao.api.response.ItemsOnsaleGetResponse;

import togoogle.common.Constant;
import togoogle.common.Util;
@Controller
public class registcontroller {
	private static final Logger log = Logger.getLogger("Util"); 

        @RequestMapping(value = "/togoogle/regist",method = RequestMethod.GET)
        public String doGet(String top_appkey, String top_session, 
        		            String top_parameters,String top_sign,String nick,String regstatus,Model model) {
        	     	
        	if(!Util.validateSign(top_sign,top_appkey+top_parameters+top_session,Constant.APP_SERCET)){
        		return "error";
        	}
        	if(regstatus.equals("0")){
        		return "regist";
        	}
			PersistenceManager pm = PMF.get().getPersistenceManager();
			try{
				acctinfo acct=new acctinfo(nick,0,new Date(),new Date());
				pm.makePersistent(acct);
			}catch(Exception e){
				pm.close();
				log.info(e.getMessage());
				return "error";
			}
			TaobaoClient cli=new DefaultTaobaoClient(Constant.SANDBOX_URL, Constant.APP_KEY, Constant.APP_SERCET);
			long pageNo=1;
			ItemsOnsaleGetRequest request=new ItemsOnsaleGetRequest();
			ItemsOnsaleGetResponse respons=null;
			request.setFields("num_iid");
        	request.setPageNo(pageNo);
        	request.setPageSize((long) 100);
        	try {
        		respons=cli.execute(request,top_session);
        		log.info(respons.getBody());
			} catch (ApiException e) {
				// TODO Auto-generated catch block
				log.info(e.getErrMsg());
				return "error";
			}
        	
        	while(respons!=null && respons.getItems()!=null && respons.getItems().size()!=0){

        		List<Item> listitems=respons.getItems();
        		for(int i=0;i<listitems.size();i++){
        			Item item=listitems.get(i);
        			long tmp=item.getNumIid();
        			try{
        				productinfo p=new productinfo(Long.toString(tmp),item.getTitle(),nick,new Date());
        				pm.makePersistent(p);
        			}catch(Exception e){
        				pm.close();
        				log.info(e.getMessage());
        				return "main";
        			}
        		}
        		pageNo++;
        		request.setPageNo(pageNo);
        		respons=null;
            	try {
            		respons=cli.execute(request);
            		if(respons==null || respons.getItems()==null || respons.getItems().size()==0){
            			break;
            		}
    			} catch (ApiException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    				break;
    			}
        	}
			model.addAttribute("regstatus", 0);
        	return "main";
        }
}